package com.bsj.power.sdk.config.hksdk.video;

import com.bsj.consumerQ.until.ExceptionUntil;
import com.bsj.power.sdk.config.hksdk.HCNetSDK;
import lombok.extern.slf4j.Slf4j;

import static com.bsj.power.sdk.config.hksdk.HKSDKClientManage.*;

/**
 * @author yinhao
 * @version 1.0
 * @description 视频直播回放跟预览
 * @date 2024/9/4
 */
@Slf4j
public class VideoPlayOp {

    /**
     * 直播预览
     * @param userID
     * @param iChannelNo
     */
    public static void realPlay(int userID, int iChannelNo, String path, String filePath) {
        if (userID == -1) {
            log.warn("请先注册");
            return;
        }
        HCNetSDK.NET_DVR_PREVIEWINFO strClientInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
        strClientInfo.read();
        //窗口句柄，从回调取流不显示一般设置为空
        strClientInfo.hPlayWnd = 0;
        //通道号
        strClientInfo.lChannel = iChannelNo;
        //0-主码流，1-子码流，2-三码流，3-虚拟码流，以此类推
        strClientInfo.dwStreamType = 0;
        //连接方式：0- TCP方式，1- UDP方式，2- 多播方式，3- RTP方式，4- RTP/RTSP，5- RTP/HTTP，6- HRUDP（可靠传输） ，7- RTSP/HTTPS，8- NPQ
        strClientInfo.dwLinkMode = 0;
        strClientInfo.bBlocked = 1;
        strClientInfo.byProtoType = 0;
        strClientInfo.write();

        //开启预览
        int lPlay = hCNetSDK.NET_DVR_RealPlay_V40(userID, strClientInfo, fRealDataCallBack, null);
        if (lPlay < 0 ) {
            int iErr = hCNetSDK.NET_DVR_GetLastError();
            log.warn("取流失败: {}", iErr);
            return;
        }
        log.info("取流成功");

        //设置裸码流回调函数
//        if (fPlayescallback==null)
//        {
//            fPlayescallback=new fPlayEScallback();
//        }
//        boolean setcallback=hCNetSDK.NET_DVR_SetESRealPlayCallBack(lPlay,fPlayescallback,null);
//        if (setcallback==false)
//        {
//            System.out.println("设置裸码流回调失败，错误码："+hCNetSDK.NET_DVR_GetLastError());
//        }

        //取流解码过程中播放库从解码码流中抓图
        getPicbyPlayCtrl(lPlay, path, filePath);

        /**
         * 预览一段时间；如果要一直取流预览，需要保证程序一直运行
         */
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            log.error("取流预览错误: {}", ExceptionUntil.getErrorStr(e));
//        }


//        String path = ".\\savevideo.mp4";
//        Boolean bSaveVideo = hCNetSDK.NET_DVR_SaveRealData_V30(lPlay, 0x2, path);
//        if (bSaveVideo == false) {
//            int iErr = hCNetSDK.NET_DVR_GetLastError();
//            System.out.println("NET_DVR_SaveRealData_V30 failed" + iErr);
//            return;
//        }
//        System.out.println("NET_DVR_SaveRealData_V30 suss");
//
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }


}
